//
//  GZ_HeaderImageVC.m
//  B段项目
//
//  Created by 高哲 on 15/11/4.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "GZ_HeaderImageVC.h"
#import "Header.h"
#import "GZ_PlayerPageVC.h"
#import "GZHeaderImageTableViewCell.h"
@interface GZ_HeaderImageVC ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataScourceArray;


@property(nonatomic, strong)XMN_ProgressHUD *progressView;

@property(nonatomic,strong)NSString *urlString;
@property(nonatomic, strong)NSString *bodyString;
@property(nonatomic, assign)NSInteger start;
@end

@implementation GZ_HeaderImageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _progressView = [[XMN_ProgressHUD alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height)];
    _progressView.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.9 alpha:1];
    _progressView.XMN_label.text = @"Waiting...";
    [self.view addSubview:_progressView];

    self.title = _radioModel.title;
   }

-(void)loadView
{
    [super loadView];
//    _urlString = @"http://api2.pianke.me/ting/radio_detail";
//    _bodyString = [NSString stringWithFormat:@"auth=&client=1&deviceid=DE384365-C3F9-468E-8E1A-3E78693E4D79&radioid=%@&version=3.0.6", _radioModel.radioid];
//    _start = 0;
    _dataScourceArray = [NSMutableArray array];
   
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView NightWithType:UIViewColorType2];
    [self.view addSubview:_tableView];
    
    
    
//    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        _start = 0;
//        [self getData];
//    }];
//    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        _start += 10;
//        [self getData];
//    }];
//
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    _tableView.header = header;
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 1; i <= 9; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%lda", i]];
        [array addObject:image];
    }
    [header setImages:array duration:1 forState:MJRefreshStatePulling];
    header.lastUpdatedTimeLabel.hidden = YES;
    [header setTitle:@"加载中 ..." forState:MJRefreshStateRefreshing];
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    _tableView.footer = footer;
    [footer setImages:array duration:1 forState:MJRefreshStateRefreshing];
    [_tableView.header beginRefreshing];
}

- (void)getData
{
    if ([_tableView.header isRefreshing]) {
        _start = 0;
    }
    else
    {
        _start += 10;
    }
    if (_start == 0) {
        _urlString = @"http://api2.pianke.me/ting/radio_detail";
        _bodyString = [NSString stringWithFormat:@"auth=&client=1&deviceid=DE384365-C3F9-468E-8E1A-3E78693E4D79&radioid=%@&version=3.0.6", _radioModel.radioid];
        //auth=&client=1&deviceid=DE384365-C3F9-468E-8E1A-3E78693E4D79&radioid=%@&version=3.0.6
        
    }else{
        _urlString = @"http://api2.pianke.me/ting/radio_detail_list";
        _bodyString = [NSString stringWithFormat:@"auth=&client=1&deviceid=DE384365-C3F9-468E-8E1A-3E78693E4D79&limit=10&radioid=%@&start=%ld&version=3.0.6", _radioModel.radioid, _start];
    }

    
[HTTPMethod getDataByString:_urlString BODYDic:[ChangeString changeStringToDictionary:_bodyString] WithDataBlock:^(id data) {
    
    NSDictionary *dataDic = [data valueForKey:@"data"];
    if (!_start) {
        //头视图
        [_dataScourceArray removeAllObjects];
        
        myView *bgView = [[myView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 270 * FitHeight)];
        
        NSDictionary *radioInfoDic = [dataDic valueForKey:@"radioInfo"];
        
        UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 180 * FitHeight)];
        [headerImageView sd_setImageWithURL:[NSURL URLWithString:[radioInfoDic valueForKey:@"coverimg"]] placeholderImage:[UIImage imageNamed:@"GZ_scroll.png"]];
        [bgView addSubview:headerImageView];
        
        UIImageView *RecommendImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20 * FitWidth, headerImageView.frame.size.height + 30 *FitHeight, 30 * FitWidth, 30 * FitHeight)];
        RecommendImageView.image = [UIImage imageNamed:@"GZtuijian.png"];
        [bgView addSubview:RecommendImageView];
        
        UILabel *RecommendLabel = [[UILabel alloc] initWithFrame:CGRectMake(RecommendImageView.frame.size.width + RecommendImageView.frame.origin.x + 5 * FitWidth, RecommendImageView.frame.origin.y + 5 * FitHeight, 120 * FitWidth, 30 * FitHeight)];
        RecommendLabel.text = @"Commend";
        RecommendLabel.textAlignment = NSTextAlignmentLeft;
        RecommendLabel.font = [UIFont fontWithName:@"Zapfino" size:16];
        RecommendLabel.textColor = [UIColor colorWithRed:0.56 green:0.6 blue:0.64 alpha:1];
        [bgView addSubview:RecommendLabel];
        
        UIImageView *singImageView = [[UIImageView alloc] initWithFrame:CGRectMake(270 * FitWidth, RecommendLabel.frame.origin.y, 20 * FitWidth, 20 * FitHeight)];
        singImageView.image = [UIImage imageNamed:@"GZguangbo.png"];
        [bgView addSubview:singImageView];
        
        UILabel *peopleLabel = [[UILabel alloc] initWithFrame:CGRectMake(singImageView.frame.origin.x + singImageView.frame.size.width + 5 * FitWidth, singImageView.frame.origin.y, 75 * FitWidth, 25 * FitHeight)];
        peopleLabel.textColor = [UIColor lightGrayColor];
        peopleLabel.font = [UIFont systemFontOfSize:12];
        peopleLabel.text = [NSString stringWithFormat:@"%@", [radioInfoDic valueForKey:@"musicvisitnum"]];
        peopleLabel.textAlignment = NSTextAlignmentLeft;
        [bgView addSubview:peopleLabel];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, bgView.frame.size.height - 1, ScreenWidth, 1)];
        lineView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
        [bgView addSubview:lineView];
        
        _tableView.tableHeaderView = bgView;
        
        NSArray *listArray = [dataDic valueForKey:@"list"];
        for (NSDictionary *listDic in listArray) {
            GZ_RadioModel *radioModel = [[GZ_RadioModel alloc] init];
            [radioModel setValuesForKeysWithDictionary:listDic];
            [_dataScourceArray addObject:radioModel];
        }
    }
    else{
        
        NSArray *listArray = [dataDic valueForKey:@"list"];
        
        for (NSDictionary *listDic in listArray) {
            GZ_RadioModel *radioModel = [[GZ_RadioModel alloc] init];
            [radioModel setValuesForKeysWithDictionary:listDic];
            [_dataScourceArray addObject:radioModel];
        }

    }
    [_tableView reloadData];
    
    [_tableView.header endRefreshing];
    [_tableView.footer endRefreshing];
    [_progressView removeFromSuperview];
}];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GZ_PlayerPageVC *ppvc = [[GZ_PlayerPageVC alloc] init];
    ppvc.dataSourceArray = _dataScourceArray;
    ppvc.tingString = [_dataScourceArray[indexPath.row] tingid];
    ppvc.tempString = _tempString;
    [self.navigationController pushViewController:ppvc animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * FitHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataScourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"GZ";
    GZHeaderImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[GZHeaderImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataScourceArray.count != 0) {
        cell.radioModel = _dataScourceArray[indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.2);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.2);
    }];
    

    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
